package de.dhbw.karlsruhe.picsim.model.cpu.impl;

import de.dhbw.karlsruhe.picsim.model.cpu.File;
import de.dhbw.karlsruhe.picsim.model.memory.RegisterValue;
import de.dhbw.karlsruhe.picsim.model.memory.impl.RegisterValueImpl;
import de.dhbw.karlsruhe.picsim.util.BitsMutable;
import de.dhbw.karlsruhe.picsim.util.impl.BitUtilities;
import de.dhbw.karlsruhe.picsim.util.impl.BitsMutableImpl;
import de.dhbw.karlsruhe.picsim.util.impl.Lengths;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: Apr 14, 2010
 * Time: 9:57:23 AM
 */
public final class FileImpl implements File {
    private final BitsMutable bits;
    private final List<Boolean> writeMask;

    public FileImpl() {
        bits = new BitsMutableImpl(BitUtilities.asLeastSignificantBitFirst(0, Lengths.FILE_LENGTH));

        //per default ist alles schreibbar
        writeMask = new ArrayList<Boolean>();
        for (int i = 0; i < Lengths.FILE_LENGTH; i++) {
            writeMask.add(true);
        }
    }

    @Override
    public void setValue(@NotNull final RegisterValue bits) {
        for (int i = 0; i < Lengths.FILE_LENGTH; i++) {
            if (writeMask.get(i)) {
                this.bits.setBit(i, bits.isSet(i));
            }
        }
    }

    @Override
    public void makeBitReadOnly(final int which) {
        writeMask.set(which, false);
    }

    @Override
    public void makeBitWriteAble(final int which) {
        writeMask.set(which, true);
    }

    @Override
    public RegisterValue getData() {
        return new RegisterValueImpl(bits);
    }
}
